package cn.weakup.hashmap;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 * @author lixj
 * @date 2022/4/28
 * @desc
 */
public class TestHashMap {
    public static void main(String[] args) {
        // 实例化无参对象
        HashMap map = new HashMap();
        Object oldVal = map.put("k", "v");
        map.putIfAbsent("k", "v2");


        // 引用赋值
        HashMap map2 = map;
        map2.put("k2", "v2");
        System.out.println(map.get("k2"));
        System.out.println(map2.get("k2"));

        HashMap map3 = new HashMap();

        map3.putAll(map);
        map3.put("k3", "v3");

        System.out.println(map.get("k3"));
        System.out.println(map3.get("k3"));


        System.out.println(comparableClassFor("111"));

    }

    static Class<?> comparableClassFor(Object x) {
        if (x instanceof Comparable) {
            Class<?> c; Type[] ts, as; Type t; ParameterizedType p;
            if ((c = x.getClass()) == String.class) // bypass checks
                return c;
            if ((ts = c.getGenericInterfaces()) != null) {
                for (int i = 0; i < ts.length; ++i) {
                    if (((t = ts[i]) instanceof ParameterizedType) &&
                            ((p = (ParameterizedType)t).getRawType() ==
                                    Comparable.class) &&
                            (as = p.getActualTypeArguments()) != null &&
                            as.length == 1 && as[0] == c) // type arg is c
                        return c;
                }
            }
        }
        return null;
    }


}
